import request from 'umi-request';
export default {
  namespace: 'employee',

  state: [],

  reducers: {
    update(_, { payload }) {
      const { employees } = payload;
      return employees;
    },
  },

  effects: {
    *getAll(_, { call, put }) {
      const response = yield call(request.get, '/api/employees');

      yield put({
        type: 'update',
        payload: {
          employees: response,
        },
      });
      return response;
    },

    *deleteEmployee({ payload }, { call, put }) {
      const { id } = payload;
      const response = yield call(request.get, '/api/delEmployee?key=' + id);

      yield put({
        type: 'update',
        payload: {
          employees: response,
        },
      });
      return response;
    },

    *selectById({ payload }, { call, put }) {
      const { key } = payload;

      const response = yield call(request.get, '/api/selectById?key=' + key);

      return response[0];
    },

    *addEmployee({ payload }, { put }) {
      const { user } = payload;
      const res = request
        .post('/api/AddEmployee', {
          data: {
            user,
          },
        })
        .then((res) => {
          put({
            type: 'update',
            payload: {
              employees: res,
            },
          });
        });
    },
  },
};
